<!-- 订单管理 -->
<template>
  <div class="app-container">
    <!-- 查询表单 -->
    <el-form :inline="true" :model="listQuery" ref="listQuery">
      <el-form-item prop="searchKey">
        <el-input
          style="width: 300px"
          v-model="listQuery.searchKey"
          placeholder="手机号/所属商户号"
        ></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="listQuery.status">
          <el-option label="全部" value="-1"></el-option>
          <el-option label="已支付" value="5"></el-option>
          <el-option label="已取消" value="10"></el-option>
          <el-option label="待支付" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单渠道" prop="purchase_Channel">
        <el-select v-model="listQuery.purchase_Channel">
          <el-option label="全部" value="-1"></el-option>
          <el-option label="平台" value="1"></el-option>
          <el-option label="线下" value="2"></el-option>
          <el-option label="推广" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付时间" prop="date">
        <el-date-picker
          v-model="listQuery.date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData(1)">查询</el-button>
        <el-button @click="resetQueryForm">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 统计 -->
    <div class="stats">
      <span
        >订单数：<i>{{ table.total }}</i></span
      >
      <span
        >成交金额：<i>{{ sumOrderMoney }}</i
        >元</span
      >
    </div>
    <!-- 表格 -->
    <el-table
      border
      :data="table.tableData"
      v-loading="table.loading"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="order_No" label="订单编号" />
      <el-table-column prop="merchant_Num" label="所属商户号" />
      <el-table-column label="订单渠道">
        <template slot-scope="scope">
          {{ purchase_ChannelEnum[scope.row.purchase_Channel] }}
        </template>
      </el-table-column>
      <el-table-column prop="mobile" label="手机号" />
      <el-table-column prop="coupon_Name" label="商品名称" />
      <el-table-column prop="actually_Money" label="订单金额（元）" />
      <el-table-column label="订单状态">
        <template slot-scope="scope">
          {{ statusEnum[scope.row.status] }}
        </template>
      </el-table-column>
      <el-table-column label="首次核销">
        <template slot-scope="scope">
          {{ scope.row.is_Use ? "是" : "否" }}
        </template>
      </el-table-column>
      <el-table-column prop="pay_Platform_Name" label="支付渠道" />
      <el-table-column label="下单时间">
        <template slot-scope="scope">
          {{ scope.row.addTime | filterTime }}
        </template>
      </el-table-column>
      <el-table-column label="清分信息" width="120">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleInfo(scope.row, 2)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="核销信息" width="120">
        <template slot-scope="scope">
          <el-button size="mini" @click="goOrderWriteOff(scope.row)"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="会员卡信息">
        <template slot-scope="scope">
          <el-button size="mini" @click="openMembershipCardInfo(scope.row)"
            >查看</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" width="170">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleInfo(scope.row, 1)"
            >查看</el-button
          >
          <el-button
            type="danger"
            size="mini"
            @click="handleRefund(scope.row)"
            v-if="scope.row.status == 5"
            >退款</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      v-show="table.total > 0"
      :total="table.total"
      :page.sync="listQuery.pageIndex"
      :limit.sync="listQuery.pageSize"
      @pagination="getData"
    />
    <!-- 订单详情、清分信息 -->
    <orderInfoDialog :orderInfo="orderInfo" :statusEnum="statusEnum" />
    <!-- 会员卡信息 -->
    <membershipCardDialog :membershipCard="membershipCard" />
  </div>
</template>

<script>
import membershipCardDialog from "./components/membershipCardDialog";
import orderInfoDialog from "./components/orderInfoDialog.vue";
export default {
  name: "Order",
  data() {
    return {
      // 订单状态枚举
      statusEnum: {
        5: "已支付",
        10: "已取消",
        1: "待支付",
        15: "退款中",
        20: "已退款"
      },
      // 订单渠道枚举
      purchase_ChannelEnum: {
        1: "平台",
        2: "线下",
        3: "推广"
      },
      // 会员卡信息
      membershipCard: {
        tableData: [],
        show: false
      },
      // 订单信息
      orderInfo: {
        rowData: {},
        tableData: [],
        show: false,
        type: 1
      },
      // 查询数据
      listQuery: {
        searchKey: "",
        purchase_Channel: "-1",
        status: "-1",
        beginTime: "",
        endTime: "",
        pageSize: 10,
        pageIndex: 1,
        date: [],
        store_Id: ""
      },
      // 表格数据
      table: {
        loading: true,
        total: 0,
        tableData: []
      },
      sumOrderMoney: 0
    };
  },
  components: {
    membershipCardDialog,
    orderInfoDialog
  },
  mounted() {
    // 投入回本跳转携带的store_Id
    if (this.$route.params.type) {
      this.listQuery.store_Id = this.$route.params.type;
    }
    // 用户管理跳转过来携带的phone
    if (this.$route.query.phone) {
      this.listQuery.searchKey = this.$route.query.phone;
    }
    this.getData();
  },
  methods: {
    // 打开会员信息详情
    async openMembershipCardInfo(row) {
      const res = await this.$api.order.GetWashCardInfoByOrderNo({
        order_No: row.order_No
      });
      this.membershipCard.tableData = [];
      this.membershipCard.tableData.push(res.data);
      this.membershipCard.show = true;
    },
    // 跳转订单核销管理页面并携带order_No
    goOrderWriteOff(row) {
      this.$router.push("/orderWriteOff/orderWriteOff?orderId=" + row.id);
    },
    // 打开订单详情
    async handleInfo(row, type) {
      const res = await this.$api.order.GetOrderDerail({
        order_No: row.order_No
      });
      this.orderInfo.tableData = res.data.proportionDatas;
      this.orderInfo.rowData = row;
      this.orderInfo.type = type;
      this.orderInfo.show = true;
    },
    // 订单退款
    handleRefund(row) {
      this.$prompt("退款原因：", "订单退款", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputValidator: val => {
          return !val ? false : true;
        },
        inputErrorMessage: "请输入退款原因"
      }).then(async ({ value }) => {
        const res = await this.$api.order.Refound({
          order_No: row.order_No,
          Reason: value
        });
        this.$message({
          type: "success",
          message: "操作成功！"
        });
      });
    },
    // 重置查询条件
    resetQueryForm() {
      this.listQuery.store_Id = "";
      this.$refs.listQuery.resetFields();
    },
    // 获取list数据
    async getData(pageIndex) {
      if (pageIndex) {
        this.listQuery.pageIndex = pageIndex;
      }
      this.table.loading = true;
      let reqObj = {
        searchKey: this.listQuery.searchKey,
        purchase_Channel: Number(this.listQuery.purchase_Channel),
        status: Number(this.listQuery.status),
        beginTime: this.listQuery.date && this.listQuery.date[0],
        endTime: this.listQuery.date && this.listQuery.date[1],
        pageSize: this.listQuery.pageSize,
        pageIndex: this.listQuery.pageIndex
      };
      if (this.listQuery.store_Id) {
        reqObj.store_Id = this.listQuery.store_Id;
      }
      if (!reqObj.beginTime) {
        delete reqObj.beginTime;
        delete reqObj.endTime;
      }
      const res = await this.$api.order.getOrderList(reqObj);
      this.table.loading = false;
      this.table.tableData = res.data.data;
      this.table.total = res.data.count;
      this.sumOrderMoney = res.data.sumOrderMoney;
    }
  }
};
</script>
<style lang="scss" scoped>
.stats {
  margin-bottom: 10px;
  span {
    margin-right: 20px;
    i {
      font-style: normal;
      font-size: 20px;
    }
  }
}
</style>
